package de.lang.rollo.test.mongodb;


import java.util.List;
import java.util.concurrent.Semaphore;

import javax.validation.Valid;

import org.mongojack.ObjectId;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinState;


public class RolloMotor {

	public String _id;

	public String name;

	public MotorState state;
	
	public long runTimeUp;
	
	public long runTimeDown;

	@JsonIgnore
	GpioPinDigitalOutput pinUpOut;
	@JsonIgnore
	GpioPinDigitalOutput pinDownOut;
	
	@JsonIgnore
	private Semaphore semaphore;

	@JsonIgnore
	public MotorRunner runnable;

	@JsonSerialize(using = PinSerializer.class)
	@JsonDeserialize(using = PinDeserializer.class)
	public Pin pinUp;

	@JsonSerialize(using = PinSerializer.class)
	@JsonDeserialize(using = PinDeserializer.class)
	public Pin pinDown;

//	@ObjectId
	public String getId() {
		return _id;
	}

//	@ObjectId
	public void setId(String id) {
		this._id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Pin getPinUp() {
		return pinUp;
	}

	public void setPinUp(Pin pinUp) {
		this.pinUp = pinUp;
	}

	public Pin getPinDown() {
		return pinDown;
	}

	public void setPinDown(Pin pinDown) {
		this.pinDown = pinDown;
	}
	
	public long getRunTimeUp() {
		return runTimeUp;
	}

	public void setRunTimeUp(long runTimeUp) {
		this.runTimeUp = runTimeUp;
	}

	public long getRunTimeDown() {
		return runTimeDown;
	}

	public void setRunTimeDown(long runTimeDown) {
		this.runTimeDown = runTimeDown;
	}

	public MotorRunner getRunnable() {
		return runnable;
	}

	public void setRunnable(MotorRunner runnable) {
		this.runnable = runnable;
	}

	public synchronized Semaphore getSemaphore() {
		return semaphore;
	}
	
	public void setSemaphore(Semaphore semaphore){
		this.semaphore = semaphore;
	}

	public synchronized MotorState getState() {
		return state;
	}

	public synchronized void setState(MotorState state) {
		this.state = state;
	}

	public GpioPinDigitalOutput getPinUpOut() {
		return pinUpOut;
	}

	public GpioPinDigitalOutput getPinDownOut() {
		return pinDownOut;
	}

	public void initGPIOPins(GpioController gpio) {
		this.pinDownOut = gpio.provisionDigitalOutputPin(pinDown, this.name
				+ "_down", PinState.HIGH);
		this.pinDownOut.setShutdownOptions(true,PinState.HIGH);
		this.pinUpOut = gpio.provisionDigitalOutputPin(pinUp, this.name
				+ "_down", PinState.HIGH);
		this.pinUpOut.setShutdownOptions(true,PinState.HIGH);
	}

}
